package com.devil.traffic.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.devil.traffic.service.TrafficService;
import com.devil.traffic.task.Task;
import com.devil.traffic.tools.Tool;

public class TrafficActivity extends Activity {

	private EditText inputNum;
	private String strInputNum;
	private ImageButton searchBtn;
	private GridView gridView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.traffic_activity);
		//用户输入车牌号码框
		inputNum = (EditText) findViewById(R.id.inputNum);
		//搜索按钮
		searchBtn = (ImageButton) findViewById(R.id.searchBtn);
		//用户收藏号码列表
		gridView = (GridView) findViewById(R.id.gridView);
		//给搜索按钮绑定监听器
		searchBtn.setOnClickListener(searchBtnClk);
		
		List<HashMap<String, String>> dataList = new ArrayList<HashMap<String,String>>();
		for(int i=0; i<5; i++){
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("remarks", "张三"+i);
			map.put("searchNumber", "D78884");
			map.put("serachResult", "共有"+i*20+"条违章记录");
			map.put("searchTime", "2013/05/15查询");
			dataList.add(map);
		}
		
		gridView.setAdapter(new SimpleAdapter(this, dataList, R.layout.number_item, new String[]{"remarks", "searchNumber", "serachResult", "searchTime"}, new int[]{R.id.remarks, R.id.searchNumber, R.id.serachResult, R.id.serachTime}));
		gridView.setOnItemClickListener(itemClick);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//获取网络状态
        Boolean chkNet = Tool.checkNetWorkStatus(this);
        if(chkNet){
	        	//网络正常则启动主服务线程
	    		Intent service = new Intent(this, TrafficService.class);
	    		startService(service);
	    		//当前activity加入到activity栈内
	    		TrafficService.allActivities.add(this);
	    		System.out.println("启动服务之后到我这里");
        }else{
	        	Tool.alerNetErr(this);
	        	return;
        }
	}

	//响应查询按钮的点击事件
	OnClickListener searchBtnClk = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			//获取用户输入的车牌号
			strInputNum = inputNum.getText().toString().trim();
			System.out.println("用户输入的车牌号是："+strInputNum);
			addSearchTask(strInputNum);
		}
	};
	
	//GridView条目点击事件
	OnItemClickListener itemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			GridView gv = (GridView) parent;
			@SuppressWarnings("unchecked")
			HashMap<String, String> map = (HashMap<String, String>) gv.getItemAtPosition(position);
			String searchNumber = map.get("searchNumber");
			System.out.println(searchNumber);
			addSearchTask(searchNumber);
		}
	};
	
	//根据车牌号码添加查询任务
	public void addSearchTask(String searchNum){
		if(!searchNum.equals("")){
			//添加查询任务
			Task task = new Task(Task.GET_SIMPLE_TRAFFIC_RECORD, searchNum);
			TrafficService.newTask(task);
			startActivity(new Intent(TrafficActivity.this, ResultActivity.class));
		}else{
			Toast.makeText(TrafficActivity.this, "请输入正确的车牌号码！", Toast.LENGTH_SHORT).show();
			return;
		}
	}
}
